package com.ruoyi.miaomiao.controller.anonymous;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.miaomiao.domain.Address;
import com.ruoyi.miaomiao.domain.FGoods;
import com.ruoyi.miaomiao.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务于用户的用户地址表的crud的Controller
 */
@Api(tags = "用户收获地址管理") //  tags：你可以当作是这个组的名字。
@RestController
@RequestMapping("/address")
@ApiResponses(value = {@ApiResponse(code = 0, message = "地址对象信息", response = Address.class)})
public class AnAddressController extends BaseController
{
    //  TODO: 以下crud都仍需要身份验证方法

    @Autowired
    private IAddressService addressService;

    /**
     * 按用户id查询地址列表
     */
    @ApiOperation(value = "按用户id批量查询地址列表",notes = "通过发送url:" +
            "\"/address/{userId}\"的方式, 按userId查询用户地址列表")
    @GetMapping("/{userId}")
    public TableDataInfo list(@PathVariable("userId") Long userId)
    {
        startPage();
        List<Address> list = addressService.selectAddressByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 获取用户地址详细信息
     */
    @ApiOperation(value = "获取用户单条地址信息",notes = "在固定用户的情况下按每条地址记录本身的id查询地址列表")
    @GetMapping(value = "/{userId}/{addressId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId, @PathVariable("addressId") Long addressId)
    {
        return AjaxResult.success(addressService.selectAddressByAddressAndUserId(userId, addressId));
    }

    /**
     * 新增用户地址
     */
    @ApiOperation(value = "新增用户地址",notes = "上传Address类型数据并新增（不建议填写addressID字段）")
    @PostMapping
    public AjaxResult add(@RequestBody Address address)
    {
        return toAjax(addressService.insertAddress(address));
    }

    /**
     * 修改用户地址
     */
    @ApiOperation(value = "修改用户收货地址",notes = "上传Address类型数据修正数据中userID和addressID对应的原数据")
    @PutMapping
    public AjaxResult edit(@RequestBody Address address)
    {
        return toAjax(addressService.updateAddress(address));
    }

    /**
     * 批量删除用户地址
     */
    @ApiOperation(value = "批量删除用户收货地址",notes = "在固定用户的情况下按所传地址id数组批量删除")
	@DeleteMapping("/{userId}/{addressIds}")
    public AjaxResult remove(@PathVariable("userId") Long userId, @PathVariable("addressIds") Long[] addressIds)
    {
        return toAjax(addressService.deleteAddressByAddressAndUserId(userId, addressIds));
    }
}